/*
 * 订单
 *
 * */

import Api from "../../api/api"
import * as types from "../mutation-types"
import  {setParams} from "../../util/methods"

const state = {
    param: {
        "name": "",
        "contract_no": "",
        "phone": "",
        "store_name": "",
        "start_at": "",
        "end_at": "",
        "status": "",
        "city": '',
        "page": 0
    },
    orderList: [],
    contract_b_d: {family: {}, product: {}, customer: {}, bank: {}, sale: {}},
    contract_no: '',
    contract_i_d: {},
    fastStatus: 0,
    searchType: 0,
    qActive: null,
    selectContractStatus: '',
    oid:0
}


const actions = {
    orderList({commit}, payload){
        return new Promise((resolve, reject) => {
            var append = payload.hasOwnProperty("append") ? payload.append : false
            if (state.searchType == 1) {
                Api.qOrderList({
                    status: state.fastStatus,
                    page: state.param.page,
                    name: state.param.name,
                    store_name: state.param.store_name,
                    phone: state.param.phone,
                }).then(res => {
                    commit({
                        type: "ORDER_LIST",
                        append: append,  //当前是快捷搜索
                        res,
                    })
                    resolve(res)
                }, err => {
                    reject(err)
                })
                    .catch(err => {
                        reject(err)
                    })
            } else {
                Api.orderList(state.param)
                    .then(res => {
                        commit({
                            type: "ORDER_LIST",
                            append: append,
                            res,
                        })
                        resolve(res)
                    }, err => {
                        reject(err)
                    })
                    .catch(err => {
                        reject(err)
                    })
            }

        })

    },

    /*
     *   合同详情  基本信息
     * */
    contractDetail({commit}, payload){
        return new Promise((resolve, reject) => {
            Api.contractDetail({contract_no: payload.contract_no}).then(res => {
                commit({
                    type: "CONTRACT_D",
                    res,
                })
                resolve(res)
            }, err => {
                reject(err)
            })
        })
    },

    /*
     * 合同详情 影像信息
     *
     * */
    contractImageDetail({commit}, payload){
        return new Promise((resolve, reject) => {
            Api.contractImage({contract_no: payload.contract_no}).then(res => {
                commit({
                    type: "CONTRACT_I",
                    res,
                })
                resolve(res)
            }, err => {
                reject(err)
            })
        })
    },

    //未提交合同详情
    uncommittedContractDetail({commit}, payload){
        return new Promise((resolve, reject) => {
            Api.uncommittedContractDetail({order_id: payload.oid}).then(res => {
                commit({
                    type: "CONTRACT_D",
                    res,
                })
                resolve(res)
            }, err => {
                reject(err)
            })
        })
    },

    //未提交合同影像
    uncommittedContractImage({commit}, payload){
        return new Promise((resolve, reject) => {
            Api.uncommittedContractImage({order_id: payload.oid}).then(res => {
                commit({
                    type: "CONTRACT_I",
                    res,
                })
                resolve(res)
            }, err => {
                reject(err)
            })
        })
    },

    /*
     * 取消订单
     * */
    cancelOrder({commit}, payload){
        console.log(payload)
        return new Promise((resolve, reject) => {
            Api.cancelOrder(payload.params).then(res => {
                resolve(res)
            }, err => {
                reject(err)
            })
        })
    },
    /*
     * 合同否决
     * */
    refuseOrder({commit}, payload){
        return new Promise((resolve, reject) => {
            Api.refuseOrder(payload.params).then(res => {
                resolve(res)
            }, err => {
                reject(err)
            })
        })
    },
    //还款计划
    orderPrepayment({commit}, payload){
        return new Promise((resolve, reject) => {
            Api.orderPrepayment(payload.params).then(res => {
                resolve(res)
            }, err => {
                reject(err)
            })
        })
    },
}

const mutations = {
    [types.ORDER_LIST](state, payload){
        if (!payload.append || (payload.res.current_page <= 1 && payload.res.data.length < 1)) {
            state.orderList = payload.res.data;
        } else {
            state.orderList = state.orderList.concat(payload.res.data)
        }

    },
    [types.CONTRACT_D](state, payload){
        state.contract_b_d = payload.res;
    },
    [types.CONTRACT_I](state, payload){
        state.contract_i_d = payload.res
    },
    [types.OID](state, payload){
        state.OID = payload.res
    },
    setParamStatus(state, payload){
        state.param.status = payload.status
    },

    //设置页数
    setPage(state, payload){
        state.param.page = payload.page
    },
    setOrderTime(state, payload){
        state.param[payload.timeType] = payload.time
    },
    setValue(state, payload){
        state.param[payload.key] = payload.value
    },
    setOrderParams(state, payload){
        state = setParams(state, payload.params)
    },
    resetParam(state){
        state.param = {
            "name": "",
            "contract_no": "",
            "phone": "",
            "store_name": "",
            "start_at": "",
            "end_at": "",
            "status": "",
            "page": 0
        };
        state.orderList = [];
        state.contract_b_d = {family: {}, product: {}, customer: {}, bank: {}, sale: {}};
        state.contract_no = '';
        state.contract_i_d = {};
        state.fastStatus = 0;
        state.searchType = 0;
        state.qActive = null;
    }

}

export default {
    state,
    actions,
    mutations,
}
